<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="card">
      <el-row :gutter="20" class="c2">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="query">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="shangpinglist()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData" stripe border="">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500px">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="50px">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品重量 " width="50px">
        </el-table-column>
        <el-table-column label="创建时间">
          <template #default="{ row }">{{
            moment(row.upd_time).format("YYYY-MM-DD HH:ss:mm")
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="bianji(row.goods_id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="shanchu(row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      class="fooer"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[10, 15, 20, 30]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      title="编辑用户"
      :visible.sync="bianjiListshow"
      width="50%"
      @close="bianjiclose"
    >
      <el-form :model="bianjiForm" status-icon ref="bianjiFormref">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input
            v-model="bianjiForm.goods_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input
            v-model="bianjiForm.goods_price"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_number">
          <el-input
            v-model="bianjiForm.goods_number"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="bianjiListshow = false">取 消</el-button>
        <el-button type="primary" @click="bianjiListshow = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment";
import { getshangpinglist, getshangpingid, getshanchu } from "@/api/shangp";
export default {
  name: "GoodS",
  data() {
    return {
      moment,
      query: "",
      pagenum: 1,
      pagesize: 10,
      tableData: [],
      total: 0,
      //编辑
      bianjiListshow: false,
      bianjiForm: {
        roleName: "",
        roleDesc: "",
      },
    };
  },
  created() {
    this.shangpinglist();
  },
  methods: {
    async shangpinglist() {
      const res = await getshangpinglist({
        pagenum: this.pagenum,
        pagesize: this.pagesize,
        query: this.query,
      });
      console.log(res);
      this.tableData = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pagesize = val;
      this.shangpinglist();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pagenum = val;
      this.shangpinglist();
    },
    async bianji(id) {
      this.bianjiListshow = true;
      // console.log(id);
      const xiu = await getshangpingid(id);
      console.log(xiu);
      // console.log(xiu);
      this.bianjiForm = xiu.data;
    },
    bianjiclose() {
      this.$refs.bianjiFormref.resetFields();
    },
    async shanchu(id) {
      const quxiao = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => {
        return err;
      });
      if (quxiao !== "confirm") {
        return this.$message.info("取消删除");
      }
      // console.log(quxiao);
      const ii = await getshanchu(id);
      console.log(ii);
      this.$message.success("删除成功");
      this.shangpinglist();
    },
  },
};
</script>

<style scoped lang="scss">
.el-breadcrumb {
  margin-bottom: 20px;
}
.c2 {
  margin-bottom: 20px;
}
</style>
